/**
 *
 */
package com.example.demo.controller;

import com.example.demo.common.GlobalException;
import com.example.demo.common.ParamData;
import com.example.demo.common.ResStatusEnum;
import com.example.demo.common.dto.LayuiData;
import com.example.demo.common.dto.ResData;
import com.example.demo.dao.BaseDao;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Map;

/**
 *
 *
 * @author Gavin<br>
 *         2020年4月24日
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private BaseDao bd;
    @Autowired
    private UserService userService;

    /**
     * @Param layui获取用户，分页查询
     * @Date 2020.06.06 11:56
     **/
    @RequestMapping("/getAllUser")
    public LayuiData selectAllBrands(HttpServletRequest request) {
        LayuiData res = new LayuiData();
        ParamData param = new ParamData(request);
        try {
            log.info("test info logger");
            log.error("test error logger");
            Map<String, Object> result = (Map<String, Object>) bd.findForPaging("UserMapper.selectAllUser", param);
            if (result != null) {
                res.setCode(0);
                res.setData((ArrayList<Object>) result.get("data"));
                res.setCount(Integer.parseInt(String.valueOf(result.get("count"))));
                res.setMsg("");
            } else {
                res.setCode(0);
                res.setData(null);
                res.setCount(0);
                res.setMsg("暂未查询到相关数据");
            }
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResStatusEnum.CODE_500.getStatus());
            res.setMsg("数据出现异常，请稍后重试！");
        }
        return res;
    }

    /**
     * @Param 获取用户接口，调试看看
     * @Date 2020.06.06 11:57
     **/
    @GetMapping("/getUser")
    public Object getUser(HttpServletRequest request) {
        ParamData pd = new ParamData(request);//pd就是前台传递的所有参数
        try {
            System.out.println(pd);
            return bd.findForList("UserMapper.selectAllUser", pd);
        } catch (Exception e) {
            e.printStackTrace();
            return ResData.ERROR_500("网络繁忙");
        }
    }

    @GetMapping("/transactionalDemo")
    public ResData transactionlDemo() {
        try {
            userService.demoA();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResData.SUCCESS();
    }
}
